﻿using System;
using System.Collections;
using System.Collections.Generic;
using Gp.Scripts.Core;
using Gp.Scripts.Data.Skills;
using Gp.Scripts.Utils;


namespace Gp.Scripts.Core {
    public class MagicSkillEntity : SkillEntity {
        // public MagicSkillEntity(ConfigSkill config) : base(config) {
        //     if (config is not Scripts.Data.Skills.MagicEntityConfig) {
        //         throw new ArgumentException("data type config error");
        //     }
        // }

        public MagicEntityConfig MagicEntityConfig => null;
        // public MagicBonusContainer BonusContainer => UnitReference.AbilityManager.MagicBonusContainer;



        public virtual int MagicCost => MagicEntityConfig.magicCost;
        public int ActualMagicCost => MagicCost;


        


        private IEnumerator ExecuteMagic(SkillContext context) {
            Global.Get<BattleSystem>().BlockInput(this);


            yield return PreProcess();


            if (!InterruptJudge())
                yield return Act(context);

            // BonusContainer.AfterMagicCast();
            Global.Get<BattleSystem>().ToLastState();


            Global.Get<BattleSystem>().UnBlockInput(this);
        }


        // 判断是否中断技能
        private bool InterruptJudge() {
            // 判断条件：当前单位是否死亡
            return Global.Get<BattleSystem>().IsUnitDestroyed(UnitReference);
        }


        private IEnumerator PreProcess() {
            // 法力消耗
            UnitReference.ComCost.ReduceResource(CostField.MagicPoint, ActualMagicCost);

            var leftPoint = UnitReference.ComCost.GetResource(CostField.MagicPoint);

            if (leftPoint >= 0) {
                yield break;
            }

            int overLoad = -leftPoint;
            this.FloatText("过载 " + overLoad);
            // 每有一点过载，则有10%的概率自爆
            if (!MathUtil.Chance((float)(Math.Pow(2, overLoad - 1) * .1))) {
                yield break;
            }

            // 对所有友方单位造成overLoad * 10点伤害
            var task = Task();

            yield return task.All();
        }


        // public override void HumanExecute(SkillContext context) {
        //     PreExecute();
        //     Global.Get<BattleSystem>().Level.AddSequenceBuffer("Skill Execute --->\t" + Config.Name, ExecuteMagic(context));
        // }


        public override IEnumerator AIExecute(SkillContext context) {
            OnPreExecute();
            yield return ExecuteMagic(context);
        }


        protected AttackActionResult MagicAtkAction(BaseUnit target, int damage,
            IEnumerable<WeakRuleWrapper<AttackActionRequest>> rules = null) {
            // damage = GetMagicBonus(MagicField.MagicDamage).ResultInt(damage);
            return this.StartAction(new AttackActionRequest(UnitReference, target, this, damage),
                rules) as AttackActionResult;
        }
    }


    public class MagicSkillEntity<T> : MagicSkillEntity where T : MagicEntityConfig {
        // public MagicSkillEntity(T data) : base(data) { }
        protected T MData => Config as T;
    }
}